package org.diysearch.dao.impls;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.diysearch.dao.interfaces.CommentDao;
import org.diysearch.models.Comment;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class CommentDaoImpl extends SqlMapClientDaoSupport implements CommentDao {
	private static final Log LOG = LogFactory.getLog(CommentDaoImpl.class);
	
	@Override
	public Comment findById(int id) throws SQLException {
		Comment c = new Comment();
		c.setId(id);
		return (Comment) getSqlMapClientTemplate().queryForObject("findCommentById",c);
	}

	@Override
	public List<Comment> findByItemAndOid(int item, int oid)
			throws SQLException {
		Comment c = new Comment();
		c.setObjectId(oid);
		c.setItem(item);
		return getSqlMapClientTemplate().queryForList("findCommentsByItemAndOid",c);
	}

	@Override
	public List<Comment> findByObject(int oid) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Comment> findByOwner(int owner) throws SQLException {
		Comment c = new Comment();
		c.setOwner(owner);
		return getSqlMapClientTemplate().queryForList("findCommentsByUid",c);
	}

	@Override
	public Comment save(Comment c) throws SQLException {
		getSqlMapClientTemplate().insert("save-comment-proc",c);
		return c;
	}

	public int delete(Comment c) throws SQLException {
		if (c.getId() > 0) {
			// deleting individual comment, by id
			int code = getSqlMapClientTemplate().update("delete-comment-proc",c);
			return getSqlMapClientTemplate().delete("delete-comment",c);
		} else {
			// deleting multiple comments by oid and item
			return getSqlMapClientTemplate().delete("delete-comments",c);
		}
	}

}
